﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using NormReference.Models;
using NormReference.DAL;

namespace NormReference.Controllers
{ 
    [Authorize]
    public class DepartmentFilterController : Controller
    {
        private NormReferenceContext db = new NormReferenceContext();

        //
        // GET: /DepartmentFilter/

        public ViewResult Index()
        {
            var departmentfilters = db.departmentFilters.Include(d => d.department).Include(d => d.comparisonGroup);
            return View(departmentfilters.ToList());
        }

        //
        // GET: /DepartmentFilter/Details/5

        public ViewResult Details(int id)
        {
            DepartmentFilter departmentfilter = db.departmentFilters.Find(id);
            return View(departmentfilter);
        }

        //
        // GET: /DepartmentFilter/Create

        public ActionResult Create()
        {
            ViewBag.departmentID = new SelectList(db.departments, "ID", "name");
            ViewBag.comparisonGroupID = new SelectList(db.groups, "ID", "name");
            return View();
        } 

        //
        // POST: /DepartmentFilter/Create

        [HttpPost]
        public ActionResult Create(DepartmentFilter departmentfilter)
        {
            if (ModelState.IsValid)
            {
                db.departmentFilters.Add(departmentfilter);
                db.SaveChanges();
                return RedirectToAction("Index");  
            }

            ViewBag.departmentID = new SelectList(db.departments, "ID", "name", departmentfilter.departmentID);
            ViewBag.comparisonGroupID = new SelectList(db.groups, "ID", "name", departmentfilter.comparisonGroupID);
            return View(departmentfilter);
        }
        
        //
        // GET: /DepartmentFilter/Edit/5
 
        public ActionResult Edit(int id)
        {
            DepartmentFilter departmentfilter = db.departmentFilters.Find(id);
            ViewBag.departmentID = new SelectList(db.departments, "ID", "name", departmentfilter.departmentID);
            ViewBag.comparisonGroupID = new SelectList(db.groups, "ID", "name", departmentfilter.comparisonGroupID);
            return View(departmentfilter);
        }

        //
        // POST: /DepartmentFilter/Edit/5

        [HttpPost]
        public ActionResult Edit(DepartmentFilter departmentfilter)
        {
            if (ModelState.IsValid)
            {
                db.Entry(departmentfilter).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.departmentID = new SelectList(db.departments, "ID", "name", departmentfilter.departmentID);
            ViewBag.comparisonGroupID = new SelectList(db.groups, "ID", "name", departmentfilter.comparisonGroupID);
            return View(departmentfilter);
        }

        //
        // GET: /DepartmentFilter/Delete/5
 
        public ActionResult Delete(int id)
        {
            DepartmentFilter departmentfilter = db.departmentFilters.Find(id);
            return View(departmentfilter);
        }

        //
        // POST: /DepartmentFilter/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {            
            DepartmentFilter departmentfilter = db.departmentFilters.Find(id);
            db.departmentFilters.Remove(departmentfilter);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}